<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Index extends Www_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->library('pagination');
    }

    /**
     * 首页
     */
    public function index()
    {
        // 手机端轮播图
        $this->load->model('banner_m');
        $banner['mobile'] = $this->banner_m->select(['type' => 1]);

        if ($banner['mobile']) {
            foreach ($banner['mobile'] as $key => $val) {
                $banner['mobile'][$key]['pic_url'] = str_replace('uploads/', "/uploads/", $val['pic_url']);
                $banner['mobile'][$key]['pic_url'] = str_replace('//uploads/', "/uploads/", $banner['mobile'][$key]['pic_url']);
            }
        }
        // 热门分类
        $this->load->model('articleCategory_m');
        $hot = $this->articleCategory_m->select(['deleted' => 0], 3, 0);

        // 新闻
        $this->load->model('account_m');
        $field            = "id,title,keyword,describe,create_time,img";
        $news['company']  = $this->account_m->select("category = 1", 3, 0, $field);
        $news['industry'] = $this->account_m->select("category = 2", 3, 0, $field);
        $news['media']    = $this->account_m->select("category = 3", 3, 0, $field);
        $news['hot']      = $this->account_m->select("is_hot= 1", 3, 0, $field);
        $data['news']     = $news;
        $data['hot']      = $hot;
        $data['banner']   = $banner;

        // pp($data);exit();
        $this->load->view('index', $data);
    }



    public function category()
    {
        $this->load->model('articleCategory_m');
        $data['cate'] = $this->articleCategory_m->select(['deleted' => 0]);
        // pp($data);exit();
        $this->load->view('category', $data);
    }

    /**
     * 产品
     */
    public function product($id = 0, $page = 1)
    {

        $this->load->model('article_m');
        $this->load->model('attachs_m');
        $this->load->model('articleCategory_m');
        $where = [];
        if ($id) {
            $where = ['category' => $id];
        }
        //分页
        $limit                      = 8;
        $config['uri_segment']      = 3;
        $config['use_page_numbers'] = true;
        $config['total_rows']       = $this->article_m->total($where);
        $config['base_url']         = base_url('product/' . $id . "/");
        $config['per_page']         = $limit;
        $config['num_links']        = 1;
        $start                      = ($page - 1) * $limit;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $products           = $this->article_m->select($where, $limit, $start);
        if (!empty($products)) {
            foreach ($products as $key => $val) {
                $access_key            = $val['access_key'];
                $imgs                  = $this->attachs_m->get_one_attachs_by($access_key);
                $products[$key]['img'] = base_url('uploads/product/' . $access_key . "/" . $imgs['file_name']);
            }
        }
        $data["products"] = $products ?? [];
        $data["cid"]      = $id;
        $data["cur_cate"] = $this->articleCategory_m->findBy(['id' => $id]);
        $data['cate']     = $this->articleCategory_m->select(['deleted' => 0]);
        // pp($data);exit();
        $this->load->view('product', $data);
    }

    /**
     * 产品详情
     */
    public function product_show($id = 0)
    {
        $this->load->model('article_m');
        $this->load->model('attachs_m');
        $this->load->model('articleCategory_m');
        $article         = $this->article_m->find($id);
        $data["product"] = $article ?? [];
        // 分类
        $cur_category         = $this->articleCategory_m->find($article['category']);
        $data["cur_category"] = $cur_category ?? [];
        // 图片
        $access_key = $article['access_key'];
        $imgs       = $this->attachs_m->get_attachs_by_access($access_key);
        if (!empty($imgs)) {
            foreach ($imgs as $key => $val) {
                $data["product"]['imgs'][] = base_url('uploads/product/' . $access_key . "/" . $val['file_name']);
            }
        }
        //获取上一篇
        // $data["pre_article"]  = $this->account_m->findBy("id < {$id}");
        // $data['next_article'] = $this->account_m->findBy("id > {$id}");
        // pp($data);
        // exit();
        $this->load->view('product_show', $data);
    }

    /**
     * 合作流程
     */
    public function price()
    {
        $this->load->view('price');
    }

    /**
     * 直通资讯
     */
    public function news($id = 1, $page = 1)
    {
        $where = ['deletetd' => 0];
        if ($id) {
            $where['category'] = $id;
        }
        $this->load->model('account_m');
        $this->load->library('pagination');
        $limit                      = 6;
        $config['uri_segment']      = 3;
        $config['use_page_numbers'] = true;
        $config['total_rows']       = $this->account_m->total($where);
        $config['base_url']         = base_url('news/' . $id . "/");
        $config['per_page']         = $limit;
        $config['num_links']        = 1;
        $start                      = ($page - 1) * $limit;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $field              = "id,category,title,describe,create_time,img";
        $data["news"]       = $this->account_m->select($where, $limit, $start, $field);
        $data["total"]      = $config['total_rows'];
        $data["id"]         = $id;

        $this->load->view('news', $data);
    }

    /**
     * 资讯详情
     */
    public function news_show($id = 0)
    {
        $this->load->model('account_m');
        $article         = $this->account_m->find($id);
        $data["article"] = $article ?? [];
        // 分类
        $cur_category         = $this->zixunCategory_m->find($article['category']);
        $data["cur_category"] = $cur_category ?? [];
        // pp($data);exit();
        $this->load->view('news_show', $data);
    }

    /**
     * 直通资讯
     */
    public function aike($id = 1, $page = 1)
    {
        $where = ['deletetd' => 0];
        if ($id) {
            $where['category'] = $id;
        }
        $this->load->model('online_m');
        $this->load->model('onlineCategory_m');
        $this->load->library('pagination');
        $limit                      = 6;
        $config['uri_segment']      = 3;
        $config['use_page_numbers'] = true;
        $config['total_rows']       = $this->online_m->total($where);
        $config['base_url']         = base_url('aike/' . $id . "/");
        $config['per_page']         = $limit;
        $config['num_links']        = 1;
        $start                      = ($page - 1) * $limit;
        $this->pagination->initialize($config);
        $data['pagination']   = $this->pagination->create_links();
        $field                = "id,category,title,describe,create_time,img";
        $data["aike"]         = $this->online_m->select($where, $limit, $start, $field);
        $data["total"]        = $config['total_rows'];
        $data["id"]           = $id;
        $cur_category         = $this->onlineCategory_m->find($id);
        $data["cur_category"] = $cur_category ?? [];

        $this->load->view('aike', $data);
    }

    /**
     * 资讯详情
     */
    public function aike_show($id = 0)
    {
        $this->load->model('online_m');
        $article        = $this->online_m->find($id);
        $data["online"] = $article ?? [];
        // 分类
        $cur_category         = $this->zixunCategory_m->find($article['category']);
        $data["cur_category"] = $cur_category ?? [];
        // pp($data);exit();
        $this->load->view('aike_show', $data);
    }

    /**
     * 单页
     */
    public function brand($id = 1)
    {
        // 获取简介
        $this->load->model('page_m');
        $page         = $this->page_m->find($id);
        $data['page'] = $page ?? "";
        $data['type'] = 0;
        $data['typeName'] = "品牌故事";
        $data['id'] = $id;
        $this->load->view('brand', $data);
    }

    public function service($id = 5)
    {
        // 获取简介
        $this->load->model('page_m');
        $page         = $this->page_m->find($id);
        $data['page'] = $page ?? "";
        $data['type'] = 1;
        $data['typeName'] = "艾克服务";
        $data['id'] = $id;

        $this->load->view('service', $data);
    }

    public function join($id = 9)
    {
        // 获取简介
        $this->load->model('page_m');
        $page         = $this->page_m->find($id);
        $data['page'] = $page ?? "";
        $data['type'] = 2;
        $data['typeName'] = "加盟艾克";
        $data['id'] = $id;
        $this->load->view('join', $data);
    }

}
